package exDraw;

import rz.dmc.R;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MovingSampleActivity extends Activity implements View.OnTouchListener 
{

	TextView _view;
	ViewGroup _root;
	private int _xDelta;
	private int _yDelta;

	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		_root = (ViewGroup)findViewById(R.id.main);
		
		
		_view = new TextView(this);
		_view.setBackgroundColor(Color.CYAN);
		_view.setText("TextView!!!!!!!!");

		RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(150, 50);
		layoutParams.leftMargin = 50;
		layoutParams.topMargin = 50;
		layoutParams.bottomMargin = -250;
		layoutParams.rightMargin = -250;
		_view.setLayoutParams(layoutParams);

		_view.setOnTouchListener(this);
		_root.addView(_view);
		
		
		EditText editText = new EditText(this);
		editText.setBackgroundColor(Color.DKGRAY);
		editText.setText("Test!");
		
		layoutParams = new RelativeLayout.LayoutParams(200, 40);
		layoutParams.leftMargin = 200;
		layoutParams.topMargin = 200;
		layoutParams.rightMargin = 0;
		layoutParams.bottomMargin = 0;
		editText.setLayoutParams(layoutParams);
		editText.setOnTouchListener(this);
		
		_root.addView(editText);
	}

	public boolean onTouch(View view, MotionEvent event) 
	{
		final int X = (int) event.getRawX();
		final int Y = (int) event.getRawY();

		switch (event.getAction() & MotionEvent.ACTION_MASK) 
		{
			case MotionEvent.ACTION_DOWN:
				RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
				_xDelta = X - lParams.leftMargin;
				_yDelta = Y - lParams.topMargin;
				break;
			case MotionEvent.ACTION_UP:
				break;
			case MotionEvent.ACTION_POINTER_DOWN:
				break;
			case MotionEvent.ACTION_POINTER_UP:
				break;
			case MotionEvent.ACTION_MOVE:
				RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
				layoutParams.leftMargin = X - _xDelta;
				layoutParams.topMargin = Y - _yDelta;
				layoutParams.rightMargin = -250;
				layoutParams.bottomMargin = -250;
				view.setLayoutParams(layoutParams);
				break;
		}
		_root.invalidate();
		return true;
	}
}
